package com.example.javatwo.service.cg.djg;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.javatwo.mapper.cg.djg.PurchaseMapper;
import com.example.javatwo.pojo.cg.djg.Cg_dsordProdComb;
import com.example.javatwo.pojo.cg.djg.Purchase;
import com.example.javatwo.pojo.cg.djg.PurchaseInfo;

@Service
public class PurchaseSerivce {
   
	@Autowired
	private PurchaseMapper purchaseMapper;
	
	/**
	 * 查看主表中共有多少条数据
	 * @return
	 */
	public String count(){
		return purchaseMapper.count();
	}
	
	/**
	 * 分页查询
	 * @param current
	 * @return
	 */
	public List<Purchase> findPaging(int current){
		List<Purchase> pur=purchaseMapper.findPaging(current);
		if(pur==null){
			return null;
		}else{
			for (Purchase purchase : pur) {
				purchase.setPurchaseInfos(purchaseMapper.listFindNO(purchase.getBillno()));							
			}
			return pur;			
		}
	}
	
	/**
	 * 新增数据和保存数据（主详）
	 * @param purchase
	 */
	public void save(Purchase purchase){
		Purchase data=purchaseMapper.findBillNo(purchase.getBillno());//查找对象	
		if(data==null){//判断该对像是否存在
			purchaseMapper.savePurchase(purchase);	//新增		
		}else{
			purchaseMapper.updatePurchase(purchase);//修改
			purchaseMapper.delPurchaseInfoBillno(purchase.getBillno());//删除祥表内容
			//删除流水表
			purchaseMapper.delcg_dsord(purchase.getBillno(), "采购订单");
		}
		for (PurchaseInfo info : purchase.getPurchaseInfos()) {	//循环新增祥表
			if(info.getDonation().equals("否")){    //判断物料是不是赠品
				Cg_dsordProdComb cg_d=new Cg_dsordProdComb();
				   //给流水类对象赋值
				    cg_d.setFtyid(purchase.getFtyid());//供应商ID
				    cg_d.setTranType("采购订单");//单别
				    cg_d.setBillDate(purchase.getBillDate());//日期别
				    cg_d.setBillno(purchase.getBillno());//单号
				    cg_d.setFactCurrencyName(purchase.getFactCurrencyName());//币别
				    cg_d.setExchRate(purchase.getExchRate());
				    cg_d.setProdID(info.getProdID());//物料编号
				    cg_d.setsQuantity(info.getsQuantity());
				    cg_d.setUnit("无");
				    cg_d.setOldPrice(info.getOldPrice());
				    cg_d.setDiscount(info.getDiscount());
				    cg_d.setPriceOFTax(purchase.getPriceOFTax());
				    cg_d.setAmount(info.getAmount());
				    cg_d.setRemark(info.getRemark());				    
				    purchaseMapper.saveCg_dsordProdComd(cg_d);    
			}
			purchaseMapper.savePurchaseInfo(purchase.getBillno(),info);
		}
	}
	
	/**
	 * 查询当日最大的单号
	 *  service中的判断，
	 * @param date
	 * @return
	 */
	public String findMaxBillno(String date){
		date=date.replaceAll("-","");//去掉-
		String billno=purchaseMapper.findMaxBillno(date);
		if(billno==null){
			return date+"001";
		}
		else{
			return (Long.parseLong(billno)+1)+"";
		}
	}
	
	/**
	 * 删除主表（逻辑删除）
	 * @param billno
	 */
	public void delPurchase(String billno){
		purchaseMapper.delPurchaseBillno(billno);
	}
	
	/**
	 * 审核，反审核
	 * @param billNo
	 */
	public void updatePurYnAudit(String billNo,String yNAudit){
		purchaseMapper.updatePurYnAudit(billNo,yNAudit);
		
	}
	
    /**
     *  历史交易查询
	 *   条件      供应商id
     * @param ftyid
     * @return
     */
	public List<Cg_dsordProdComb> find_cg_jycx(String ftyid){
		return purchaseMapper.find_cg_jycx(ftyid);
	}
}
